home *** CD-ROM | disk | FTP | other *** search
/ SGI Freeware 1998 November / Freeware November 1998.img / dist / fw_elisp-manual-19.idb / usr / freeware / info / elisp-15.z / elisp-15 (.txt)
GNU Info File  |  1998-05-26  |  51KB  |  948 lines

  1. This is Info file elisp, produced by Makeinfo-1.63 from the input file
  2. elisp.texi.
  3.    This version is the edition 2.4.2 of the GNU Emacs Lisp Reference
  4. Manual.  It corresponds to Emacs Version 19.34.
  5.    Published by the Free Software Foundation 59 Temple Place, Suite 330
  6. Boston, MA  02111-1307  USA
  7.    Copyright (C) 1990, 1991, 1992, 1993, 1994, 1995, 1996 Free Software
  8. Foundation, Inc.
  9.    Permission is granted to make and distribute verbatim copies of this
  10. manual provided the copyright notice and this permission notice are
  11. preserved on all copies.
  12.    Permission is granted to copy and distribute modified versions of
  13. this manual under the conditions for verbatim copying, provided that the
  14. entire resulting derived work is distributed under the terms of a
  15. permission notice identical to this one.
  16.    Permission is granted to copy and distribute translations of this
  17. manual into another language, under the above conditions for modified
  18. versions, except that this permission notice may be stated in a
  19. translation approved by the Foundation.
  20.    Permission is granted to copy and distribute modified versions of
  21. this manual under the conditions for verbatim copying, provided also
  22. that the section entitled "GNU General Public License" is included
  23. exactly as in the original, and provided that the entire resulting
  24. derived work is distributed under the terms of a permission notice
  25. identical to this one.
  26.    Permission is granted to copy and distribute translations of this
  27. manual into another language, under the above conditions for modified
  28. versions, except that the section entitled "GNU General Public License"
  29. may be included in a translation approved by the Free Software
  30. Foundation instead of in the original English.
  31. File: elisp,  Node: Using Interactive,  Next: Interactive Codes,  Up: Defining Commands
  32. Using `interactive'
  33. -------------------
  34.    This section describes how to write the `interactive' form that
  35. makes a Lisp function an interactively-callable command.
  36.  - Special Form: interactive ARG-DESCRIPTOR
  37.      This special form declares that the function in which it appears
  38.      is a command, and that it may therefore be called interactively
  39.      (via `M-x' or by entering a key sequence bound to it).  The
  40.      argument ARG-DESCRIPTOR declares how to compute the arguments to
  41.      the command when the command is called interactively.
  42.      A command may be called from Lisp programs like any other
  43.      function, but then the caller supplies the arguments and
  44.      ARG-DESCRIPTOR has no effect.
  45.      The `interactive' form has its effect because the command loop
  46.      (actually, its subroutine `call-interactively') scans through the
  47.      function definition looking for it, before calling the function.
  48.      Once the function is called, all its body forms including the
  49.      `interactive' form are executed, but at this time `interactive'
  50.      simply returns `nil' without even evaluating its argument.
  51.    There are three possibilities for the argument ARG-DESCRIPTOR:
  52.    * It may be omitted or `nil'; then the command is called with no
  53.      arguments.  This leads quickly to an error if the command requires
  54.      one or more arguments.
  55.    * It may be a Lisp expression that is not a string; then it should
  56.      be a form that is evaluated to get a list of arguments to pass to
  57.      the command.
  58.      If this expression reads keyboard input (this includes using the
  59.      minibuffer), keep in mind that the integer value of point or the
  60.      mark before reading input may be incorrect after reading input.
  61.      This is because the current buffer may be receiving subprocess
  62.      output; if subprocess output arrives while the command is waiting
  63.      for input, it could relocate point and the mark.
  64.      Here's an example of what *not* to do:
  65.           (interactive
  66.            (list (region-beginning) (region-end)
  67.                  (read-string "Foo: " nil 'my-history)))
  68.      Here's how to avoid the problem, by examining point and the mark
  69.      only after reading the keyboard input:
  70.           (interactive
  71.            (let ((string (read-string "Foo: " nil 'my-history)))
  72.              (list (region-beginning) (region-end) string)))
  73.    * It may be a string; then its contents should consist of a code
  74.      character followed by a prompt (which some code characters use and
  75.      some ignore).  The prompt ends either with the end of the string
  76.      or with a newline.  Here is a simple example:
  77.           (interactive "bFrobnicate buffer: ")
  78.      The code letter `b' says to read the name of an existing buffer,
  79.      with completion.  The buffer name is the sole argument passed to
  80.      the command.  The rest of the string is a prompt.
  81.      If there is a newline character in the string, it terminates the
  82.      prompt.  If the string does not end there, then the rest of the
  83.      string should contain another code character and prompt,
  84.      specifying another argument.  You can specify any number of
  85.      arguments in this way.
  86.      The prompt string can use `%' to include previous argument values
  87.      (starting with the first argument) in the prompt.  This is done
  88.      using `format' (*note Formatting Strings::.).  For example, here
  89.      is how you could read the name of an existing buffer followed by a
  90.      new name to give to that buffer:
  91.           (interactive "bBuffer to rename: \nsRename buffer %s to: ")
  92.      If the first character in the string is `*', then an error is
  93.      signaled if the buffer is read-only.
  94.      If the first character in the string is `@', and if the key
  95.      sequence used to invoke the command includes any mouse events, then
  96.      the window associated with the first of those events is selected
  97.      before the command is run.
  98.      You can use `*' and `@' together; the order does not matter.
  99.      Actual reading of arguments is controlled by the rest of the prompt
  100.      string (starting with the first character that is not `*' or `@').
  101. File: elisp,  Node: Interactive Codes,  Next: Interactive Examples,  Prev: Using Interactive,  Up: Defining Commands
  102. Code Characters for `interactive'
  103. ---------------------------------
  104.    The code character descriptions below contain a number of key words,
  105. defined here as follows:
  106. Completion
  107.      Provide completion.  TAB, SPC, and RET perform name completion
  108.      because the argument is read using `completing-read' (*note
  109.      Completion::.).  `?' displays a list of possible completions.
  110. Existing
  111.      Require the name of an existing object.  An invalid name is not
  112.      accepted; the commands to exit the minibuffer do not exit if the
  113.      current input is not valid.
  114. Default
  115.      A default value of some sort is used if the user enters no text in
  116.      the minibuffer.  The default depends on the code character.
  117. No I/O
  118.      This code letter computes an argument without reading any input.
  119.      Therefore, it does not use a prompt string, and any prompt string
  120.      you supply is ignored.
  121.      Even though the code letter doesn't use a prompt string, you must
  122.      follow it with a newline if it is not the last code character in
  123.      the string.
  124. Prompt
  125.      A prompt immediately follows the code character.  The prompt ends
  126.      either with the end of the string or with a newline.
  127. Special
  128.      This code character is meaningful only at the beginning of the
  129.      interactive string, and it does not look for a prompt or a newline.
  130.      It is a single, isolated character.
  131.    Here are the code character descriptions for use with `interactive':
  132.      Signal an error if the current buffer is read-only.  Special.
  133.      Select the window mentioned in the first mouse event in the key
  134.      sequence that invoked this command.  Special.
  135.      A function name (i.e., a symbol satisfying `fboundp').  Existing,
  136.      Completion, Prompt.
  137.      The name of an existing buffer.  By default, uses the name of the
  138.      current buffer (*note Buffers::.).  Existing, Completion, Default,
  139.      Prompt.
  140.      A buffer name.  The buffer need not exist.  By default, uses the
  141.      name of a recently used buffer other than the current buffer.
  142.      Completion, Default, Prompt.
  143.      A character.  The cursor does not move into the echo area.  Prompt.
  144.      A command name (i.e., a symbol satisfying `commandp').  Existing,
  145.      Completion, Prompt.
  146.      The position of point, as an integer (*note Point::.).  No I/O.
  147.      A directory name.  The default is the current default directory of
  148.      the current buffer, `default-directory' (*note System
  149.      Environment::.).  Existing, Completion, Default, Prompt.
  150.      The first or next mouse event in the key sequence that invoked the
  151.      command.  More precisely, `e' gets events that are lists, so you
  152.      can look at the data in the lists.  *Note Input Events::.  No I/O.
  153.      You can use `e' more than once in a single command's interactive
  154.      specification.  If the key sequence that invoked the command has N
  155.      events that are lists, the Nth `e' provides the Nth such event.
  156.      Events that are not lists, such as function keys and ASCII
  157.      characters, do not count where `e' is concerned.
  158.      A file name of an existing file (*note File Names::.).  The default
  159.      directory is `default-directory'.  Existing, Completion, Default,
  160.      Prompt.
  161.      A file name.  The file need not exist.  Completion, Default,
  162.      Prompt.
  163.      A key sequence (*note Keymap Terminology::.).  This keeps reading
  164.      events until a command (or undefined command) is found in the
  165.      current key maps.  The key sequence argument is represented as a
  166.      string or vector.  The cursor does not move into the echo area.
  167.      Prompt.
  168.      This kind of input is used by commands such as `describe-key' and
  169.      `global-set-key'.
  170.      A key sequence, whose definition you intend to change.  This works
  171.      like `k', except that it suppresses, for the last input event in
  172.      the key sequence, the conversions that are normally used (when
  173.      necessary) to convert an undefined key into a defined one.
  174.      The position of the mark, as an integer.  No I/O.
  175.      A number read with the minibuffer.  If the input is not a number,
  176.      the user is asked to try again.  The prefix argument, if any, is
  177.      not used.  Prompt.
  178.      The numeric prefix argument; but if there is no prefix argument,
  179.      read a number as with `n'.  Requires a number.  *Note Prefix
  180.      Command Arguments::.  Prompt.
  181.      The numeric prefix argument.  (Note that this `p' is lower case.)
  182.      No I/O.
  183.      The raw prefix argument.  (Note that this `P' is upper case.)  No
  184.      I/O.
  185.      Point and the mark, as two numeric arguments, smallest first.
  186.      This is the only code letter that specifies two successive
  187.      arguments rather than one.  No I/O.
  188.      Arbitrary text, read in the minibuffer and returned as a string
  189.      (*note Text from Minibuffer::.).  Terminate the input with either
  190.      LFD or RET.  (`C-q' may be used to include either of these
  191.      characters in the input.)  Prompt.
  192.      An interned symbol whose name is read in the minibuffer.  Any
  193.      whitespace character terminates the input.  (Use `C-q' to include
  194.      whitespace in the string.)  Other characters that normally
  195.      terminate a symbol (e.g., parentheses and brackets) do not do so
  196.      here.  Prompt.
  197.      A variable declared to be a user option (i.e., satisfying the
  198.      predicate `user-variable-p').  *Note High-Level Completion::.
  199.      Existing, Completion, Prompt.
  200.      A Lisp object, specified with its read syntax, terminated with a
  201.      LFD or RET.  The object is not evaluated.  *Note Object from
  202.      Minibuffer::.  Prompt.
  203.      A Lisp form is read as with `x', but then evaluated so that its
  204.      value becomes the argument for the command.  Prompt.
  205. File: elisp,  Node: Interactive Examples,  Prev: Interactive Codes,  Up: Defining Commands
  206. Examples of Using `interactive'
  207. -------------------------------
  208.    Here are some examples of `interactive':
  209.      (defun foo1 ()              ; `foo1' takes no arguments,
  210.          (interactive)           ;   just moves forward two words.
  211.          (forward-word 2))
  212.           => foo1
  213.      
  214.      (defun foo2 (n)             ; `foo2' takes one argument,
  215.          (interactive "p")       ;   which is the numeric prefix.
  216.          (forward-word (* 2 n)))
  217.           => foo2
  218.      
  219.      (defun foo3 (n)             ; `foo3' takes one argument,
  220.          (interactive "nCount:") ;   which is read with the Minibuffer.
  221.          (forward-word (* 2 n)))
  222.           => foo3
  223.      
  224.      (defun three-b (b1 b2 b3)
  225.        "Select three existing buffers.
  226.      Put them into three windows, selecting the last one."
  227.          (interactive "bBuffer1:\nbBuffer2:\nbBuffer3:")
  228.          (delete-other-windows)
  229.          (split-window (selected-window) 8)
  230.          (switch-to-buffer b1)
  231.          (other-window 1)
  232.          (split-window (selected-window) 8)
  233.          (switch-to-buffer b2)
  234.          (other-window 1)
  235.          (switch-to-buffer b3))
  236.           => three-b
  237.      (three-b "*scratch*" "declarations.texi" "*mail*")
  238.           => nil
  239. File: elisp,  Node: Interactive Call,  Next: Command Loop Info,  Prev: Defining Commands,  Up: Command Loop
  240. Interactive Call
  241. ================
  242.    After the command loop has translated a key sequence into a
  243. definition, it invokes that definition using the function
  244. `command-execute'.  If the definition is a function that is a command,
  245. `command-execute' calls `call-interactively', which reads the arguments
  246. and calls the command.  You can also call these functions yourself.
  247.  - Function: commandp OBJECT
  248.      Returns `t' if OBJECT is suitable for calling interactively; that
  249.      is, if OBJECT is a command.  Otherwise, returns `nil'.
  250.      The interactively callable objects include strings and vectors
  251.      (treated as keyboard macros), lambda expressions that contain a
  252.      top-level call to `interactive', byte-code function objects made
  253.      from such lambda expressions, autoload objects that are declared
  254.      as interactive (non-`nil' fourth argument to `autoload'), and some
  255.      of the primitive functions.
  256.      A symbol is `commandp' if its function definition is `commandp'.
  257.      Keys and keymaps are not commands.  Rather, they are used to look
  258.      up commands (*note Keymaps::.).
  259.      See `documentation' in *Note Accessing Documentation::, for a
  260.      realistic example of using `commandp'.
  261.  - Function: call-interactively COMMAND &optional RECORD-FLAG
  262.      This function calls the interactively callable function COMMAND,
  263.      reading arguments according to its interactive calling
  264.      specifications.  An error is signaled if COMMAND is not a function
  265.      or if it cannot be called interactively (i.e., is not a command).
  266.      Note that keyboard macros (strings and vectors) are not accepted,
  267.      even though they are considered commands, because they are not
  268.      functions.
  269.      If RECORD-FLAG is non-`nil', then this command and its arguments
  270.      are unconditionally added to the list `command-history'.
  271.      Otherwise, the command is added only if it uses the minibuffer to
  272.      read an argument.  *Note Command History::.
  273.  - Function: command-execute COMMAND &optional RECORD-FLAG
  274.      This function executes COMMAND as an editing command.  The
  275.      argument COMMAND must satisfy the `commandp' predicate; i.e., it
  276.      must be an interactively callable function or a keyboard macro.
  277.      A string or vector as COMMAND is executed with
  278.      `execute-kbd-macro'.  A function is passed to
  279.      `call-interactively', along with the optional RECORD-FLAG.
  280.      A symbol is handled by using its function definition in its place.
  281.      A symbol with an `autoload' definition counts as a command if it
  282.      was declared to stand for an interactively callable function.
  283.      Such a definition is handled by loading the specified library and
  284.      then rechecking the definition of the symbol.
  285.  - Command: execute-extended-command PREFIX-ARGUMENT
  286.      This function reads a command name from the minibuffer using
  287.      `completing-read' (*note Completion::.).  Then it uses
  288.      `command-execute' to call the specified command.  Whatever that
  289.      command returns becomes the value of `execute-extended-command'.
  290.      If the command asks for a prefix argument, it receives the value
  291.      PREFIX-ARGUMENT.  If `execute-extended-command' is called
  292.      interactively, the current raw prefix argument is used for
  293.      PREFIX-ARGUMENT, and thus passed on to whatever command is run.
  294.      `execute-extended-command' is the normal definition of `M-x', so
  295.      it uses the string `M-x ' as a prompt.  (It would be better to
  296.      take the prompt from the events used to invoke
  297.      `execute-extended-command', but that is painful to implement.)  A
  298.      description of the value of the prefix argument, if any, also
  299.      becomes part of the prompt.
  300.           (execute-extended-command 1)
  301.           ---------- Buffer: Minibuffer ----------
  302.           1 M-x forward-word RET
  303.           ---------- Buffer: Minibuffer ----------
  304.                => t
  305.  - Function: interactive-p
  306.      This function returns `t' if the containing function (the one whose
  307.      code includes the call to `interactive-p') was called
  308.      interactively, with the function `call-interactively'.  (It makes
  309.      no difference whether `call-interactively' was called from Lisp or
  310.      directly from the editor command loop.)  If the containing
  311.      function was called by Lisp evaluation (or with `apply' or
  312.      `funcall'), then it was not called interactively.
  313.      The most common use of `interactive-p' is for deciding whether to
  314.      print an informative message.  As a special exception,
  315.      `interactive-p' returns `nil' whenever a keyboard macro is being
  316.      run.  This is to suppress the informative messages and speed
  317.      execution of the macro.
  318.      For example:
  319.           (defun foo ()
  320.             (interactive)
  321.             (and (interactive-p)
  322.                  (message "foo")))
  323.                => foo
  324.           
  325.           (defun bar ()
  326.             (interactive)
  327.             (setq foobar (list (foo) (interactive-p))))
  328.                => bar
  329.           
  330.           ;; Type `M-x foo'.
  331.                -| foo
  332.           
  333.           ;; Type `M-x bar'.
  334.           ;; This does not print anything.
  335.           
  336.           foobar
  337.                => (nil t)
  338. File: elisp,  Node: Command Loop Info,  Next: Input Events,  Prev: Interactive Call,  Up: Command Loop
  339. Information from the Command Loop
  340. =================================
  341.    The editor command loop sets several Lisp variables to keep status
  342. records for itself and for commands that are run.
  343.  - Variable: last-command
  344.      This variable records the name of the previous command executed by
  345.      the command loop (the one before the current command).  Normally
  346.      the value is a symbol with a function definition, but this is not
  347.      guaranteed.
  348.      The value is copied from `this-command' when a command returns to
  349.      the command loop, except when the command specifies a prefix
  350.      argument for the following command.
  351.      This variable is always local to the current terminal and cannot be
  352.      buffer-local.  *Note Multiple Displays::.
  353.  - Variable: this-command
  354.      This variable records the name of the command now being executed by
  355.      the editor command loop.  Like `last-command', it is normally a
  356.      symbol with a function definition.
  357.      The command loop sets this variable just before running a command,
  358.      and copies its value into `last-command' when the command finishes
  359.      (unless the command specifies a prefix argument for the following
  360.      command).
  361.      Some commands set this variable during their execution, as a flag
  362.      for whatever command runs next.  In particular, the functions for
  363.      killing text set `this-command' to `kill-region' so that any kill
  364.      commands immediately following will know to append the killed text
  365.      to the previous kill.
  366.    If you do not want a particular command to be recognized as the
  367. previous command in the case where it got an error, you must code that
  368. command to prevent this.  One way is to set `this-command' to `t' at the
  369. beginning of the command, and set `this-command' back to its proper
  370. value at the end, like this:
  371.      (defun foo (args...)
  372.        (interactive ...)
  373.        (let ((old-this-command this-command))
  374.          (setq this-command t)
  375.          ...do the work...
  376.          (setq this-command old-this-command)))
  377.  - Function: this-command-keys
  378.      This function returns a string or vector containing the key
  379.      sequence that invoked the present command, plus any previous
  380.      commands that generated the prefix argument for this command.  The
  381.      value is a string if all those events were characters.  *Note
  382.      Input Events::.
  383.           (this-command-keys)
  384.           ;; Now use `C-u C-x C-e' to evaluate that.
  385.                => "^U^X^E"
  386.  - Variable: last-nonmenu-event
  387.      This variable holds the last input event read as part of a key
  388.      sequence, not counting events resulting from mouse menus.
  389.      One use of this variable is to figure out a good default location
  390.      to pop up another menu.
  391.  - Variable: last-command-event
  392.  - Variable: last-command-char
  393.      This variable is set to the last input event that was read by the
  394.      command loop as part of a command.  The principal use of this
  395.      variable is in `self-insert-command', which uses it to decide which
  396.      character to insert.
  397.           last-command-event
  398.           ;; Now use `C-u C-x C-e' to evaluate that.
  399.                => 5
  400.      The value is 5 because that is the ASCII code for `C-e'.
  401.      The alias `last-command-char' exists for compatibility with Emacs
  402.      version 18.
  403.  - Variable: last-event-frame
  404.      This variable records which frame the last input event was
  405.      directed to.  Usually this is the frame that was selected when the
  406.      event was generated, but if that frame has redirected input focus
  407.      to another frame, the value is the frame to which the event was
  408.      redirected.  *Note Input Focus::.
  409. File: elisp,  Node: Input Events,  Next: Reading Input,  Prev: Command Loop Info,  Up: Command Loop
  410. Input Events
  411. ============
  412.    The Emacs command loop reads a sequence of "input events" that
  413. represent keyboard or mouse activity.  The events for keyboard activity
  414. are characters or symbols; mouse events are always lists.  This section
  415. describes the representation and meaning of input events in detail.
  416.  - Function: eventp OBJECT
  417.      This function returns non-`nil' if OBJECT is an input event.
  418. * Menu:
  419. * Keyboard Events::        Ordinary characters-keys with symbols on them.
  420. * Function Keys::        Function keys-keys with names, not symbols.
  421. * Mouse Events::                Overview of mouse events.
  422. * Click Events::        Pushing and releasing a mouse button.
  423. * Drag Events::            Moving the mouse before releasing the button.
  424. * Button-Down Events::        A button was pushed and not yet released.
  425. * Repeat Events::               Double and triple click (or drag, or down).
  426. * Motion Events::        Just moving the mouse, not pushing a button.
  427. * Focus Events::        Moving the mouse between frames.
  428. * Misc Events::                 Other events window systems can generate.
  429. * Event Examples::        Examples of the lists for mouse events.
  430. * Classifying Events::        Finding the modifier keys in an event symbol.
  431.                 Event types.
  432. * Accessing Events::        Functions to extract info from events.
  433. * Strings of Events::           Special considerations for putting
  434.                   keyboard character events in a string.
  435. File: elisp,  Node: Keyboard Events,  Next: Function Keys,  Up: Input Events
  436. Keyboard Events
  437. ---------------
  438.    There are two kinds of input you can get from the keyboard: ordinary
  439. keys, and function keys.  Ordinary keys correspond to characters; the
  440. events they generate are represented in Lisp as characters.  In Emacs
  441. versions 18 and earlier, characters were the only events.  The event
  442. type of a character event is the character itself (an integer); see
  443. *Note Classifying Events::.
  444.    An input character event consists of a "basic code" between 0 and
  445. 255, plus any or all of these "modifier bits":
  446.      The 2**27 bit in the character code indicates a character typed
  447.      with the meta key held down.
  448. control
  449.      The 2**26 bit in the character code indicates a non-ASCII control
  450.      character.
  451.      ASCII control characters such as `C-a' have special basic codes of
  452.      their own, so Emacs needs no special bit to indicate them.  Thus,
  453.      the code for `C-a' is just 1.
  454.      But if you type a control combination not in ASCII, such as `%'
  455.      with the control key, the numeric value you get is the code for
  456.      `%' plus 2**26 (assuming the terminal supports non-ASCII control
  457.      characters).
  458. shift
  459.      The 2**25 bit in the character code indicates an ASCII control
  460.      character typed with the shift key held down.
  461.      For letters, the basic code indicates upper versus lower case; for
  462.      digits and punctuation, the shift key selects an entirely different
  463.      character with a different basic code.  In order to keep within
  464.      the ASCII character set whenever possible, Emacs avoids using the
  465.      2**25 bit for those characters.
  466.      However, ASCII provides no way to distinguish `C-A' from `C-a', so
  467.      Emacs uses the 2**25 bit in `C-A' and not in `C-a'.
  468. hyper
  469.      The 2**24 bit in the character code indicates a character typed
  470.      with the hyper key held down.
  471. super
  472.      The 2**23 bit in the character code indicates a character typed
  473.      with the super key held down.
  474.      The 2**22 bit in the character code indicates a character typed
  475.      with the alt key held down.  (On some terminals, the key labeled
  476.      ALT is actually the meta key.)
  477.    It is best to avoid mentioning specific bit numbers in your program.
  478. To test the modifier bits of a character, use the function
  479. `event-modifiers' (*note Classifying Events::.).  When making key
  480. bindings, you can use the read syntax for characters with modifier bits
  481. (`\C-', `\M-', and so on).  For making key bindings with `define-key',
  482. you can use lists such as `(control hyper ?x)' to specify the
  483. characters (*note Changing Key Bindings::.).  The function
  484. `event-convert-list' converts such a list into an event type (*note
  485. Classifying Events::.).
  486. File: elisp,  Node: Function Keys,  Next: Mouse Events,  Prev: Keyboard Events,  Up: Input Events
  487. Function Keys
  488. -------------
  489.    Most keyboards also have "function keys"--keys that have names or
  490. symbols that are not characters.  Function keys are represented in Lisp
  491. as symbols; the symbol's name is the function key's label, in lower
  492. case.  For example, pressing a key labeled F1 places the symbol `f1' in
  493. the input stream.
  494.    The event type of a function key event is the event symbol itself.
  495. *Note Classifying Events::.
  496.    Here are a few special cases in the symbol-naming convention for
  497. function keys:
  498. `backspace', `tab', `newline', `return', `delete'
  499.      These keys correspond to common ASCII control characters that have
  500.      special keys on most keyboards.
  501.      In ASCII, `C-i' and TAB are the same character.  If the terminal
  502.      can distinguish between them, Emacs conveys the distinction to
  503.      Lisp programs by representing the former as the integer 9, and the
  504.      latter as the symbol `tab'.
  505.      Most of the time, it's not useful to distinguish the two.  So
  506.      normally `function-key-map' (*note Translating Input::.) is set up
  507.      to map `tab' into 9.  Thus, a key binding for character code 9 (the
  508.      character `C-i') also applies to `tab'.  Likewise for the other
  509.      symbols in this group.  The function `read-char' likewise converts
  510.      these events into characters.
  511.      In ASCII, BS is really `C-h'.  But `backspace' converts into the
  512.      character code 127 (DEL), not into code 8 (BS).  This is what most
  513.      users prefer.
  514. `left', `up', `right', `down'
  515.      Cursor arrow keys
  516. `kp-add', `kp-decimal', `kp-divide', ...
  517.      Keypad keys (to the right of the regular keyboard).
  518. `kp-0', `kp-1', ...
  519.      Keypad keys with digits.
  520. `kp-f1', `kp-f2', `kp-f3', `kp-f4'
  521.      Keypad PF keys.
  522. `kp-home', `kp-left', `kp-up', `kp-right', `kp-down'
  523.      Keypad arrow keys.  Emacs normally translates these into the
  524.      non-keypad keys `home', `left', ...
  525. `kp-prior', `kp-next', `kp-end', `kp-begin', `kp-insert', `kp-delete'
  526.      Additional keypad duplicates of keys ordinarily found elsewhere.
  527.      Emacs normally translates these into the like-named non-keypad
  528.      keys.
  529.    You can use the modifier keys ALT, CTRL, HYPER, META, SHIFT, and
  530. SUPER with function keys.  The way to represent them is with prefixes
  531. in the symbol name:
  532.      The alt modifier.
  533.      The control modifier.
  534.      The hyper modifier.
  535.      The meta modifier.
  536.      The shift modifier.
  537.      The super modifier.
  538.    Thus, the symbol for the key F3 with META held down is `M-f3'.  When
  539. you use more than one prefix, we recommend you write them in
  540. alphabetical order; but the order does not matter in arguments to the
  541. key-binding lookup and modification functions.
  542. File: elisp,  Node: Mouse Events,  Next: Click Events,  Prev: Function Keys,  Up: Input Events
  543. Mouse Events
  544. ------------
  545.    Emacs supports four kinds of mouse events: click events, drag events,
  546. button-down events, and motion events.  All mouse events are represented
  547. as lists.  The CAR of the list is the event type; this says which mouse
  548. button was involved, and which modifier keys were used with it.  The
  549. event type can also distinguish double or triple button presses (*note
  550. Repeat Events::.).  The rest of the list elements give position and
  551. time information.
  552.    For key lookup, only the event type matters: two events of the same
  553. type necessarily run the same command.  The command can access the full
  554. values of these events using the `e' interactive code.  *Note
  555. Interactive Codes::.
  556.    A key sequence that starts with a mouse event is read using the
  557. keymaps of the buffer in the window that the mouse was in, not the
  558. current buffer.  This does not imply that clicking in a window selects
  559. that window or its buffer--that is entirely under the control of the
  560. command binding of the key sequence.
  561. File: elisp,  Node: Click Events,  Next: Drag Events,  Prev: Mouse Events,  Up: Input Events
  562. Click Events
  563. ------------
  564.    When the user presses a mouse button and releases it at the same
  565. location, that generates a "click" event.  Mouse click events have this
  566. form:
  567.      (EVENT-TYPE
  568.       (WINDOW BUFFER-POS (X . Y) TIMESTAMP)
  569.       CLICK-COUNT)
  570.    Here is what the elements normally mean:
  571. EVENT-TYPE
  572.      This is a symbol that indicates which mouse button was used.  It is
  573.      one of the symbols `mouse-1', `mouse-2', ..., where the buttons
  574.      are numbered left to right.
  575.      You can also use prefixes `A-', `C-', `H-', `M-', `S-' and `s-'
  576.      for modifiers alt, control, hyper, meta, shift and super, just as
  577.      you would with function keys.
  578.      This symbol also serves as the event type of the event.  Key
  579.      bindings describe events by their types; thus, if there is a key
  580.      binding for `mouse-1', that binding would apply to all events whose
  581.      EVENT-TYPE is `mouse-1'.
  582. WINDOW
  583.      This is the window in which the click occurred.
  584.      These are the pixel-denominated coordinates of the click, relative
  585.      to the top left corner of WINDOW, which is `(0 . 0)'.
  586. BUFFER-POS
  587.      This is the buffer position of the character clicked on.
  588. TIMESTAMP
  589.      This is the time at which the event occurred, in milliseconds.
  590.      (Since this value wraps around the entire range of Emacs Lisp
  591.      integers in about five hours, it is useful only for relating the
  592.      times of nearby events.)
  593. CLICK-COUNT
  594.      This is the number of rapid repeated presses so far of the same
  595.      mouse button.  *Note Repeat Events::.
  596.    The meanings of BUFFER-POS, X and Y are somewhat different when the
  597. event location is in a special part of the screen, such as the mode
  598. line or a scroll bar.
  599.    If the location is in a scroll bar, then BUFFER-POS is the symbol
  600. `vertical-scroll-bar' or `horizontal-scroll-bar', and the pair `(X .
  601. Y)' is replaced with a pair `(PORTION . WHOLE)', where PORTION is the
  602. distance of the click from the top or left end of the scroll bar, and
  603. WHOLE is the length of the entire scroll bar.
  604.    If the position is on a mode line or the vertical line separating
  605. WINDOW from its neighbor to the right, then BUFFER-POS is the symbol
  606. `mode-line' or `vertical-line'.  For the mode line, Y does not have
  607. meaningful data.  For the vertical line, X does not have meaningful
  608. data.
  609.    In one special case, BUFFER-POS is a list containing a symbol (one
  610. of the symbols listed above) instead of just the symbol.  This happens
  611. after the imaginary prefix keys for the event are inserted into the
  612. input stream.  *Note Key Sequence Input::.
  613. File: elisp,  Node: Drag Events,  Next: Button-Down Events,  Prev: Click Events,  Up: Input Events
  614. Drag Events
  615. -----------
  616.    With Emacs, you can have a drag event without even changing your
  617. clothes.  A "drag event" happens every time the user presses a mouse
  618. button and then moves the mouse to a different character position before
  619. releasing the button.  Like all mouse events, drag events are
  620. represented in Lisp as lists.  The lists record both the starting mouse
  621. position and the final position, like this:
  622.      (EVENT-TYPE
  623.       (WINDOW1 BUFFER-POS1 (X1 . Y1) TIMESTAMP1)
  624.       (WINDOW2 BUFFER-POS2 (X2 . Y2) TIMESTAMP2)
  625.       CLICK-COUNT)
  626.    For a drag event, the name of the symbol EVENT-TYPE contains the
  627. prefix `drag-'.  The second and third elements of the event give the
  628. starting and ending position of the drag.  Aside from that, the data
  629. have the same meanings as in a click event (*note Click Events::.).  You
  630. can access the second element of any mouse event in the same way, with
  631. no need to distinguish drag events from others.
  632.    The `drag-' prefix follows the modifier key prefixes such as `C-'
  633. and `M-'.
  634.    If `read-key-sequence' receives a drag event that has no key
  635. binding, and the corresponding click event does have a binding, it
  636. changes the drag event into a click event at the drag's starting
  637. position.  This means that you don't have to distinguish between click
  638. and drag events unless you want to.
  639. File: elisp,  Node: Button-Down Events,  Next: Repeat Events,  Prev: Drag Events,  Up: Input Events
  640. Button-Down Events
  641. ------------------
  642.    Click and drag events happen when the user releases a mouse button.
  643. They cannot happen earlier, because there is no way to distinguish a
  644. click from a drag until the button is released.
  645.    If you want to take action as soon as a button is pressed, you need
  646. to handle "button-down" events.(1)  These occur as soon as a button is
  647. pressed.  They are represented by lists that look exactly like click
  648. events (*note Click Events::.), except that the EVENT-TYPE symbol name
  649. contains the prefix `down-'.  The `down-' prefix follows modifier key
  650. prefixes such as `C-' and `M-'.
  651.    The function `read-key-sequence', and therefore the Emacs command
  652. loop as well, ignore any button-down events that don't have command
  653. bindings.  This means that you need not worry about defining button-down
  654. events unless you want them to do something.  The usual reason to define
  655. a button-down event is so that you can track mouse motion (by reading
  656. motion events) until the button is released.  *Note Motion Events::.
  657.    ---------- Footnotes ----------
  658.    (1)  Button-down is the conservative antithesis of drag.
  659. File: elisp,  Node: Repeat Events,  Next: Motion Events,  Prev: Button-Down Events,  Up: Input Events
  660. Repeat Events
  661. -------------
  662.    If you press the same mouse button more than once in quick succession
  663. without moving the mouse, Emacs generates special "repeat" mouse events
  664. for the second and subsequent presses.
  665.    The most common repeat events are "double-click" events.  Emacs
  666. generates a double-click event when you click a button twice; the event
  667. happens when you release the button (as is normal for all click events).
  668.    The event type of a double-click event contains the prefix
  669. `double-'.  Thus, a double click on the second mouse button with meta
  670. held down comes to the Lisp program as `M-double-mouse-2'.  If a
  671. double-click event has no binding, the binding of the corresponding
  672. ordinary click event is used to execute it.  Thus, you need not pay
  673. attention to the double click feature unless you really want to.
  674.    When the user performs a double click, Emacs generates first an
  675. ordinary click event, and then a double-click event.  Therefore, you
  676. must design the command binding of the double click event to assume
  677. that the single-click command has already run.  It must produce the
  678. desired results of a double click, starting from the results of a
  679. single click.
  680.    This is convenient, if the meaning of a double click somehow "builds
  681. on" the meaning of a single click--which is recommended user interface
  682. design practice for double clicks.
  683.    If you click a button, then press it down again and start moving the
  684. mouse with the button held down, then you get a "double-drag" event
  685. when you ultimately release the button.  Its event type contains
  686. `double-drag' instead of just `drag'.  If a double-drag event has no
  687. binding, Emacs looks for an alternate binding as if the event were an
  688. ordinary drag.
  689.    Before the double-click or double-drag event, Emacs generates a
  690. "double-down" event when the user presses the button down for the
  691. second time.  Its event type contains `double-down' instead of just
  692. `down'.  If a double-down event has no binding, Emacs looks for an
  693. alternate binding as if the event were an ordinary button-down event.
  694. If it finds no binding that way either, the double-down event is
  695. ignored.
  696.    To summarize, when you click a button and then press it again right
  697. away, Emacs generates a down event and a click event for the first
  698. click, a double-down event when you press the button again, and finally
  699. either a double-click or a double-drag event.
  700.    If you click a button twice and then press it again, all in quick
  701. succession, Emacs generates a "triple-down" event, followed by either a
  702. "triple-click" or a "triple-drag".  The event types of these events
  703. contain `triple' instead of `double'.  If any triple event has no
  704. binding, Emacs uses the binding that it would use for the corresponding
  705. double event.
  706.    If you click a button three or more times and then press it again,
  707. the events for the presses beyond the third are all triple events.
  708. Emacs does not have separate event types for quadruple, quintuple, etc.
  709. events.  However, you can look at the event list to find out precisely
  710. how many times the button was pressed.
  711.  - Function: event-click-count EVENT
  712.      This function returns the number of consecutive button presses
  713.      that led up to EVENT.  If EVENT is a double-down, double-click or
  714.      double-drag event, the value is 2.  If EVENT is a triple event,
  715.      the value is 3 or greater.  If EVENT is an ordinary mouse event
  716.      (not a repeat event), the value is 1.
  717.  - Variable: double-click-time
  718.      To generate repeat events, successive mouse button presses must be
  719.      at the same screen position, and the number of milliseconds between
  720.      successive button presses must be less than the value of
  721.      `double-click-time'.  Setting `double-click-time' to `nil'
  722.      disables multi-click detection entirely.  Setting it to `t'
  723.      removes the time limit; Emacs then detects multi-clicks by
  724.      position only.
  725. File: elisp,  Node: Motion Events,  Next: Focus Events,  Prev: Repeat Events,  Up: Input Events
  726. Motion Events
  727. -------------
  728.    Emacs sometimes generates "mouse motion" events to describe motion
  729. of the mouse without any button activity.  Mouse motion events are
  730. represented by lists that look like this:
  731.      (mouse-movement
  732.       (WINDOW BUFFER-POS (X . Y) TIMESTAMP))
  733.    The second element of the list describes the current position of the
  734. mouse, just as in a click event (*note Click Events::.).
  735.    The special form `track-mouse' enables generation of motion events
  736. within its body.  Outside of `track-mouse' forms, Emacs does not
  737. generate events for mere motion of the mouse, and these events do not
  738. appear.
  739.  - Special Form: track-mouse BODY...
  740.      This special form executes BODY, with generation of mouse motion
  741.      events enabled.  Typically BODY would use `read-event' to read the
  742.      motion events and modify the display accordingly.
  743.      When the user releases the button, that generates a click event.
  744.      Typically, BODY should return when it sees the click event, and
  745.      discard that event.
  746. File: elisp,  Node: Focus Events,  Next: Misc Events,  Prev: Motion Events,  Up: Input Events
  747. Focus Events
  748. ------------
  749.    Window systems provide general ways for the user to control which
  750. window gets keyboard input.  This choice of window is called the
  751. "focus".  When the user does something to switch between Emacs frames,
  752. that generates a "focus event".  The normal definition of a focus event,
  753. in the global keymap, is to select a new frame within Emacs, as the user
  754. would expect.  *Note Input Focus::.
  755.    Focus events are represented in Lisp as lists that look like this:
  756.      (switch-frame NEW-FRAME)
  757. where NEW-FRAME is the frame switched to.
  758.    Most X window managers are set up so that just moving the mouse into
  759. a window is enough to set the focus there.  Emacs appears to do this,
  760. because it changes the cursor to solid in the new frame.  However, there
  761. is no need for the Lisp program to know about the focus change until
  762. some other kind of input arrives.  So Emacs generates a focus event only
  763. when the user actually types a keyboard key or presses a mouse button in
  764. the new frame; just moving the mouse between frames does not generate a
  765. focus event.
  766.    A focus event in the middle of a key sequence would garble the
  767. sequence.  So Emacs never generates a focus event in the middle of a key
  768. sequence.  If the user changes focus in the middle of a key
  769. sequence--that is, after a prefix key--then Emacs reorders the events
  770. so that the focus event comes either before or after the multi-event key
  771. sequence, and not within it.
  772. File: elisp,  Node: Misc Events,  Next: Event Examples,  Prev: Focus Events,  Up: Input Events
  773. Miscellaneous Window System Events
  774. ----------------------------------
  775.    A few other event types represent occurrences within the window
  776. system.
  777. `(delete-frame (FRAME))'
  778.      This kind of event indicates that the user gave the window manager
  779.      a command to delete a particular window, which happens to be an
  780.      Emacs frame.
  781.      The standard definition of the `delete-frame' event is to delete
  782.      FRAME.
  783. `(iconify-frame (FRAME))'
  784.      This kind of event indicates that the user iconified FRAME using
  785.      the window manager.  Its standard definition is `ignore'; since the
  786.      frame has already been iconified, Emacs has no work to do.  The
  787.      purpose of this event type is so that you can keep track of such
  788.      events if you want to.
  789. `(make-frame-visible (FRAME))'
  790.      This kind of event indicates that the user deiconified FRAME using
  791.      the window manager.  Its standard definition is `ignore'; since the
  792.      frame has already been made visible, Emacs has no work to do.
  793.    If one of these events arrives in the middle of a key sequence--that
  794. is, after a prefix key--then Emacs reorders the events so that this
  795. event comes either before or after the multi-event key sequence, not
  796. within it.
  797. File: elisp,  Node: Event Examples,  Next: Classifying Events,  Prev: Misc Events,  Up: Input Events
  798. Event Examples
  799. --------------
  800.    If the user presses and releases the left mouse button over the same
  801. location, that generates a sequence of events like this:
  802.      (down-mouse-1 (#<window 18 on NEWS> 2613 (0 . 38) -864320))
  803.      (mouse-1      (#<window 18 on NEWS> 2613 (0 . 38) -864180))
  804.    While holding the control key down, the user might hold down the
  805. second mouse button, and drag the mouse from one line to the next.
  806. That produces two events, as shown here:
  807.      (C-down-mouse-2 (#<window 18 on NEWS> 3440 (0 . 27) -731219))
  808.      (C-drag-mouse-2 (#<window 18 on NEWS> 3440 (0 . 27) -731219)
  809.                      (#<window 18 on NEWS> 3510 (0 . 28) -729648))
  810.    While holding down the meta and shift keys, the user might press the
  811. second mouse button on the window's mode line, and then drag the mouse
  812. into another window.  That produces a pair of events like these:
  813.      (M-S-down-mouse-2 (#<window 18 on NEWS> mode-line (33 . 31) -457844))
  814.      (M-S-drag-mouse-2 (#<window 18 on NEWS> mode-line (33 . 31) -457844)
  815.                        (#<window 20 on carlton-sanskrit.tex> 161 (33 . 3)
  816.                         -453816))
  817. File: elisp,  Node: Classifying Events,  Next: Accessing Events,  Prev: Event Examples,  Up: Input Events
  818. Classifying Events
  819. ------------------
  820.    Every event has an "event type", which classifies the event for key
  821. binding purposes.  For a keyboard event, the event type equals the
  822. event value; thus, the event type for a character is the character, and
  823. the event type for a function key symbol is the symbol itself.  For
  824. events that are lists, the event type is the symbol in the CAR of the
  825. list.  Thus, the event type is always a symbol or a character.
  826.    Two events of the same type are equivalent where key bindings are
  827. concerned; thus, they always run the same command.  That does not
  828. necessarily mean they do the same things, however, as some commands look
  829. at the whole event to decide what to do.  For example, some commands use
  830. the location of a mouse event to decide where in the buffer to act.
  831.    Sometimes broader classifications of events are useful.  For example,
  832. you might want to ask whether an event involved the META key,
  833. regardless of which other key or mouse button was used.
  834.    The functions `event-modifiers' and `event-basic-type' are provided
  835. to get such information conveniently.
  836.  - Function: event-modifiers EVENT
  837.      This function returns a list of the modifiers that EVENT has.  The
  838.      modifiers are symbols; they include `shift', `control', `meta',
  839.      `alt', `hyper' and `super'.  In addition, the modifiers list of a
  840.      mouse event symbol always contains one of `click', `drag', and
  841.      `down'.
  842.      The argument EVENT may be an entire event object, or just an event
  843.      type.
  844.      Here are some examples:
  845.           (event-modifiers ?a)
  846.                => nil
  847.           (event-modifiers ?\C-a)
  848.                => (control)
  849.           (event-modifiers ?\C-%)
  850.                => (control)
  851.           (event-modifiers ?\C-\S-a)
  852.                => (control shift)
  853.           (event-modifiers 'f5)
  854.                => nil
  855.           (event-modifiers 's-f5)
  856.                => (super)
  857.           (event-modifiers 'M-S-f5)
  858.                => (meta shift)
  859.           (event-modifiers 'mouse-1)
  860.                => (click)
  861.           (event-modifiers 'down-mouse-1)
  862.                => (down)
  863.      The modifiers list for a click event explicitly contains `click',
  864.      but the event symbol name itself does not contain `click'.
  865.  - Function: event-basic-type EVENT
  866.      This function returns the key or mouse button that EVENT
  867.      describes, with all modifiers removed.  For example:
  868.           (event-basic-type ?a)
  869.                => 97
  870.           (event-basic-type ?A)
  871.                => 97
  872.           (event-basic-type ?\C-a)
  873.                => 97
  874.           (event-basic-type ?\C-\S-a)
  875.                => 97
  876.           (event-basic-type 'f5)
  877.                => f5
  878.           (event-basic-type 's-f5)
  879.                => f5
  880.           (event-basic-type 'M-S-f5)
  881.                => f5
  882.           (event-basic-type 'down-mouse-1)
  883.                => mouse-1
  884.  - Function: mouse-movement-p OBJECT
  885.      This function returns non-`nil' if OBJECT is a mouse movement
  886.      event.
  887.  - Function: event-convert-list LIST
  888.      This function converts a list of modifier names and a basic event
  889.      type to an event type which specifies all of them.  For example,
  890.           (event-convert-list '(control ?a))
  891.                => 1
  892.           (event-convert-list '(control meta ?a))
  893.                => -134217727
  894.           (event-convert-list '(control super f1))
  895.                => C-s-f1
  896. File: elisp,  Node: Accessing Events,  Next: Strings of Events,  Prev: Classifying Events,  Up: Input Events
  897. Accessing Events
  898. ----------------
  899.    This section describes convenient functions for accessing the data in
  900. a mouse button or motion event.
  901.    These two functions return the starting or ending position of a
  902. mouse-button event.  The position is a list of this form:
  903.      (WINDOW BUFFER-POSITION (X . Y) TIMESTAMP)
  904.  - Function: event-start EVENT
  905.      This returns the starting position of EVENT.
  906.      If EVENT is a click or button-down event, this returns the
  907.      location of the event.  If EVENT is a drag event, this returns the
  908.      drag's starting position.
  909.  - Function: event-end EVENT
  910.      This returns the ending position of EVENT.
  911.      If EVENT is a drag event, this returns the position where the user
  912.      released the mouse button.  If EVENT is a click or button-down
  913.      event, the value is actually the starting position, which is the
  914.      only position such events have.
  915.    These five functions take a position as described above, and return
  916. various parts of it.
  917.  - Function: posn-window POSITION
  918.      Return the window that POSITION is in.
  919.  - Function: posn-point POSITION
  920.      Return the buffer position in POSITION.  This is an integer.
  921.  - Function: posn-x-y POSITION
  922.      Return the pixel-based x and y coordinates in POSITION, as a cons
  923.      cell `(X . Y)'.
  924.  - Function: posn-col-row POSITION
  925.      Return the row and column (in units of characters) of POSITION, as
  926.      a cons cell `(COL . ROW)'.  These are computed from the X and Y
  927.      values actually found in POSITION.
  928.  - Function: posn-timestamp POSITION
  929.      Return the timestamp in POSITION.
  930.  - Function: scroll-bar-event-ratio EVENT
  931.      This function returns the fractional vertical position of a scroll
  932.      bar event within the scroll bar.  The value is a cons cell
  933.      `(PORTION . WHOLE)' containing two integers whose ratio is the
  934.      fractional position.
  935.  - Function: scroll-bar-scale RATIO TOTAL
  936.      This function multiplies (in effect) RATIO by TOTAL, rounding the
  937.      result to an integer.  The argument RATIO is not a number, but
  938.      rather a pair `(NUM . DENOM)'--typically a value returned by
  939.      `scroll-bar-event-ratio'.
  940.      This function is handy for scaling a position on a scroll bar into
  941.      a buffer position.  Here's how to do that:
  942.           (+ (point-min)
  943.              (scroll-bar-scale
  944.                 (posn-x-y (event-start event))
  945.                 (- (point-max) (point-min))))
  946.      Recall that scroll bar events have two integers forming ratio in
  947.      place of a pair of x and y coordinates.
  948.